package com.example.mpandroidchartdemo;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.github.mikephil.charting.charts.BubbleChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BubbleData;
import com.github.mikephil.charting.data.BubbleDataSet;
import com.github.mikephil.charting.data.BubbleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;
import java.util.List;

public class BubbleChartActivity extends AppCompatActivity {

    private BubbleChart mBubbleChart;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bubble_chart_activity);
        initView();
    }

    private void initView() {
        mBubbleChart = findViewById(R.id.bubble_chart);
        mBubbleChart.getDescription().setEnabled(false);
        mBubbleChart.setTouchEnabled(true);
        mBubbleChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        mBubbleChart.getAxisRight().setEnabled(false);
        setData();
    }

    private void setData() {
        List<BubbleEntry> list1 = new ArrayList<>();
        List<BubbleEntry> list2 = new ArrayList<>();
        List<BubbleEntry> list3 = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            float val = (float) (Math.random() * 30);
            float size = (float) (Math.random() * 40);
            list1.add(new BubbleEntry(i, val, size));
        }
        for (int i = 0; i < 20; i++) {
            float val = (float) (Math.random() * 40);
            float size = (float) (Math.random() * 50);
            list2.add(new BubbleEntry(i + 0.33F, val, size));
        }
        for (int i = 0; i < 30; i++) {
            float val = (float) (Math.random() * 50);
            float size = (float) (Math.random() * 60);
            list3.add(new BubbleEntry(i + 0.66F, val, size));
        }
        BubbleDataSet bubbleDataSet1 = new BubbleDataSet(list1,"语文");
        bubbleDataSet1.setColor(Color.RED);
        bubbleDataSet1.setDrawValues(true); // 是否显示气泡上的数值
        BubbleDataSet bubbleDataSet2 = new BubbleDataSet(list2,"数学");
        bubbleDataSet2.setColor(Color.GREEN);
        bubbleDataSet2.setDrawValues(true);
        BubbleDataSet bubbleDataSet3 = new BubbleDataSet(list3,"英语");
        bubbleDataSet3.setColor(Color.YELLOW);
        bubbleDataSet3.setDrawValues(true);
        // 添加数据
        List<IBubbleDataSet> dataSets = new ArrayList<>();
        dataSets.add(bubbleDataSet1);
        dataSets.add(bubbleDataSet2);
        dataSets.add(bubbleDataSet3);
        BubbleData bubbleData = new BubbleData(dataSets);
        mBubbleChart.setData(bubbleData);
    }

}
